import {debounce} from '@/until';
import Vue from "vue";
Vue.mixin({
  data() {
    return {
      tableHeight: "auto"
    };
  },
  mounted() {
    if (!this.$refs.remoteTable) return;
    this.autoResetTableHeight();
    window.addEventListener("resize", debounce(this.autoResetTableHeight, 300));

  },
  destroyed() {
    if (!this.$refs.remoteTable) return;
    window.removeEventListener("resize", debounce(this.autoResetTableHeight,300));
  },
  deactivated() {
    if (!this.$refs.remoteTable) return;
    window.removeEventListener("resize", debounce(this.autoResetTableHeight,300));
  },
  methods: {
    getreal(el, pname = "document") {
      let left = el.offsetLeft;
      let top = el.offsetTop;
      if (el.offsetParent && el.offsetParent.tagName.toLowerCase() !== pname) {
        //默认一致计算到document
        let p = this.getreal(el.offsetParent, pname);
        left += p[0];
        top += p[1];
      }
      return [left, top];
    },
    autoResetTableHeight() {
      setTimeout(() => {
        if (!this.$refs.remoteTable) return;
        this.tableHeight = 0;
        this.$nextTick(() => {
          let _tableHeight = window.innerHeight - this.getreal(this.$refs.remoteTable.$el)[1];
          this.tableHeight = _tableHeight < 350 ? 350 : _tableHeight;
        });
      }, 0);
    }
  }
});
